/**
 * 贪心算法-摆动序列-力扣376
 */
public class Practice_6 {
    public static void main(String[] args) {
        int[] nums={1,2,3,4,5,6,7,8,9};
        int result = wiggleMaxLength(nums);
        System.out.println(result);
    }

    public static int wiggleMaxLength(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        if (nums != null && nums.length == 1) {
            return 1;
        }
        if (nums != null && nums.length == 2) {
            if (nums[0] - nums[1] != 0) {
                return 2;//不同则取2个摆动
            } else {
                return 1;//相同则取1个摆动
            }
        }
        int result = 1;//注意只要有数据（nums.length>=1），至少有一个摆动
        if (nums != null && nums.length >= 3) {

            int prediff = 0;
            int curdiff = 0;
            for (int i = 1; i < nums.length ; i++) {
                curdiff = nums[i] - nums[i - 1];
                if (curdiff > 0 && prediff <= 0 ||
                        curdiff < 0 && prediff >= 0
                ) {
                    result = result + 1;
                    prediff = curdiff;//出现弯折才改动prediff，否则不改动prediff
                }
            }


        }
        return result;
    }
}
